﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Devices;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Threading;
using System.IO.IsolatedStorage;
using System.IO;

namespace cameraforhtml
{
    public partial class CameraViewfinder : System.Windows.Controls.UserControl
    {
        private PhotoCamera _camera = null;

        public CameraViewfinder()
        {
            InitializeComponent();
            StartCamera();
        }

        public void StartCamera()
        {
            if (_camera == null)
            {
                _camera = new PhotoCamera(CameraType.Primary);
                _camera.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(CameraInitialized);
                _camera.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs>(_camera_CaptureImageAvailable);
                viewfinderBrush.SetSource(_camera);
            }
        }

        public void CaptureImage()
        {

            // take picture
            _camera.CaptureImage();

        }

        void _camera_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
        {
            // Save picture as JPEG to isolated storage.
            using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream targetStream = isStore.OpenFile("html_content/capturedimage.jpg", FileMode.Create, FileAccess.Write))
                {
                    // Initialize the buffer for 4KB disk pages.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the image to isolated storage. 
                    while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        targetStream.Write(readBuffer, 0, bytesRead);
                    }
                }
            }
        }

        public void StopCamera()
        {
            if (_camera != null)
            {
                // Dispose camera to minimize power consumption and to expedite shutdown.
                _camera.Dispose();
                _camera = null;
            }
        }


        private void CameraInitialized(object sender, CameraOperationCompletedEventArgs e)
        {
            if (_camera != null)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    // Set the orientation of the viewfinder.
                    //this.viewfinderBrushTransformation.Angle = _camera.Orientation;
                });

            }
        }


    }
}
